import utils
from requests import Session
from urllib import parse

session = Session()
session.trust_env = False
session.adapters.DEFAULT_RETRIES=10
session.headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 Edg/92.0.902.67"}

id_map = utils.read_data("data/scraper/id-map.json")

def get_wiki_id(title:str,strip=True,quote=True)->str:
    """ 将a标签内的title的值转换为真实可用的wiki_id """
    if title is None or title == "":
        raise ValueError("title不能为空")
    if title not in id_map:
        resp = session.get(f"https://ark.fandom.com/wiki/{title}")
        id = resp.url.split("/wiki/",maxsplit=1)[-1]
        id_map[title] = id
        utils.save_data(id_map, "data/scraper/id-map.json")
    if strip:
        id = id_map[title].split("#")[0]
    if quote:
        # 对冒号等特殊字符进行转码
        id = parse.unquote_plus(id)
        id = parse.quote_plus(id)
    else:
        id = id_map[title]
    return id

def format_wiki_id(wiki_id:str)->str:
    """ 对wiki_id进行一些特殊处理 """
    # 对id进行url解码
    id = parse.unquote_plus(wiki_id)
    # 去掉id中的参数
    id = id.split("#",maxsplit=1)[0]
    # 替换id中的特殊字符
    id = id.replace(":", "_")
    id = id.replace("/", "_")
    return id

def get_id(title:str)->str:
    wiki_id = get_wiki_id(title,strip=True,quote=False)
    return format_wiki_id(wiki_id)

def get_title(wiki_id:str)->str:
    title = parse.unquote_plus(wiki_id)
    title = title.split("/",maxsplit=-1)[-1]
    title = title.replace("_", " ")
    return title